import java.rmi.*;
import java.awt.*;
import java.awt.event.*;

/**
 * A class that will upload a file to the remote server
 **/
public class FileSender implements ActionListener 
{
	private Frame frame;
	private FileReceiver receiver;
	
	public FileSender() throws Exception 
	{
		// get a handle to the remote object
		receiver = (FileReceiver)Naming.lookup( "rmi://rees.uwaterloo.ca:8080/receiver" );
        
		// make my GUI
		frame = new Frame( "File Sender" );
		frame.setBounds( 100, 100, 80, 80 );
		Button b = new Button( "Send File" );
		b.addActionListener( this );
		frame.add( "Center", b );
		frame.setVisible( true );
  }
  
  public void actionPerformed( ActionEvent event )
  {
	  try
	  {
		  // we are only interested in the send button
		  FileDialog dialog = new FileDialog( frame, "Upload a file", FileDialog.LOAD );
		  // should block until choice is made
		  dialog.setVisible( true );
		  // choice was made
		  String directoryName = dialog.getDirectory();
		  String fileName = dialog.getFile();
		  
		  System.out.println( "Sending: " + fileName );
		  FilePacket packet = new FilePacket(directoryName, fileName);
		  packet.readIn();
		  receiver.receiveFile( packet );
	  }
	  catch( Exception e )
	  {
		  e.printStackTrace();
	  }
  }
    
  public static void main( String args[] ) throws Exception 
  {
	  String usage = "Usage: java FileSender";
	  try
	  {
		  // make an object of my type
		  FileSender sender = new FileSender();
	  }
	  catch( Exception e )
	  {
		  e.printStackTrace();
		  System.out.println( usage );
	  }
  }			
} // class FileSender
